import pygame
from pygame.locals import *

class PieceStream(object):

    def __init__(self,pieces):
        self.pieces = pieces
        self.rect = pygame.Rect(685,10,65,65*9)
        self.start = 0
        self.scroll_box = pygame.Rect(786,5,9,590)
        self.scroll = pygame.Rect(786,5,9,590)
        self.scroll_mod = 0
        self.scrolling = False
        print "En pieceStream"
        self._update()

    def _update(self):

        size = len(self.pieces)
        if size<=9:
            self.start = 0
        else:
            if size-self.start<9:
                self.start = size-9
        if self.start < 0:
            self.start = 0

        if size<=9:
            s = 5
            e = 590
        else:
            p = 590.0/float(size)
            s = (self.start*p)+5
            e = (9.0*p) + 1
        
        self.scroll.y = s
        self.scroll.h = e

        y = self.rect.y
        for p in self.get_page():
            p.set_pos(self.rect.x - 5, y)            #y+=96
            y+= 65
        
        print "En Update "
            
    def down(self,cnt=1):
        print "Down"
        if self.scrolling:
            print "Down and Scrolling"
            return
        self.start+=cnt
        self._update()

    def up(self,cnt=1):
        print "UP"
        if self.scrolling:
            print "up and scrolling"
            return
        self.start-=cnt
        self._update()

    def get_page(self):  
        p = self.pieces[self.start:][:9]
        return p        
    
    def is_scroll_hit(self,pos):
        if self.scroll.collidepoint(pos):
            self.scrolling = True
            self.scroll_mod = pos[1]-self.scroll.top
            print "Scroll hit" 
            return True
        else:
            if self.scroll_box.collidepoint(pos):
                print "Else del scroll hit"
                if pos[1] < self.scroll.top:
                    self.up(9)
                    print "Scroll hit up"
                else:
                    self.down(9)
                    print "Scroll hit down"
        return False

    def do_scroll(self,pos):
        p = 590.0/float(len(self.pieces))
        print "Do scroll"
        self.start = int(round((float(pos[1]-self.scroll_mod)-5.0)/p))
        self._update()

    def stop_scroll(self):
        self.scrolling = False
        print "Stop scroll"

    def remove(self,piece):
        self.pieces.remove(piece)
        self._update()
        print "Remove"

    def drop(self,piece):
        print "Drop"
        if not piece.is_piece():
            print "Piece is piece --- PieceStream"
            return False            
        
        if self.rect.colliderect(piece.rect):
            print "Colliderect"
            inserted = False
        
            for p in self.get_page()[::-1]:
                if p.rect.colliderect(piece.rect):
                    self.pieces.insert(self.pieces.index(p),piece)
                    inserted = True
                    break
            
            if not inserted:
                self.pieces.append(piece)
                print "colliderect not incerted "
            
            self._update()
            return True
        
        return False

    def get_piece_at(self,pos):
        print "get piece at"
        if self.scrolling:
            print "get piece at scrolling"
            return None
        for p in self.get_page():
            if p.is_hit(pos):
                return p
        return None

    def draw(self,dest):
        dest.fill((255,255,255),self.scroll)     
                
        for p in self.get_page():            
            p.draw(dest)
